package com.zelin.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by WF on 2020/10/22 14:47
 */
public class MyBatisUtils {

    //1.得到SqlSessionFactyory对象
    private  SqlSessionFactory getSqlSessionFactory(String configName) throws IOException {
        //1.1)定义输入流
        InputStream inputStram = Resources.getResourceAsStream(configName);
        //1.2)构造sqlSessionFactory对象并返回
        return new SqlSessionFactoryBuilder().build(inputStram);
    }

    //2.得到SqlSession对象
    public  SqlSession getSqlSession(boolean autoCommit) throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory("mybatis/mybatis-config.xml");
        return sqlSessionFactory.openSession(autoCommit);
    }


}
